package com.huike;


import java.util.concurrent.*;

public class ScheduledDemo {

    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

        Runnable runnableTask = new Runnable() {
            public void run() {
                System.out.println("Task executed at " + System.currentTimeMillis());
            }
        };

        Callable<String> callableTask = new Callable<String>() {
            public String call() throws Exception {
                System.out.println("Task executed at " + System.currentTimeMillis());
                return "result";
            }
        };

        scheduledExecutorService.schedule(runnableTask, 5, TimeUnit.SECONDS);

        scheduledExecutorService.scheduleAtFixedRate(runnableTask, 5, 1, TimeUnit.SECONDS);

        try {
            String result = scheduledExecutorService.schedule(callableTask, 5, TimeUnit.SECONDS).get();
            System.out.println("Task result is " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }

        scheduledExecutorService.shutdown();
    }
}